#!/usr/bin/perl -w

#==============================================================================
# sortcvs
# File ID: 440141ba-5d44-11df-8e85-90e6ba3022ac
# Sorts the output from "cvs log" into chronological order.
# Made by Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#==============================================================================

use strict;
use Getopt::Std;

our ($opt_a, $opt_h) =
    (     0,      0);

getopts('ah');

$opt_h && print_help(0);

my ($curr_date, $curr_rev) = ("", "");
my @Curr = ();
my %Entry = ();
my $Rev = "";
my $Line = "";

while (<>) {
	$Line = $_;
	if ($Line =~ /^----------------------------$/) {
		if (length($Rev) && scalar(@Curr)) {
			$Entry{$Rev} = join("", @Curr);
		}
		@Curr = ();
		push(@Curr, $Line);
		$Line = <>;
		if ($Line =~ /^revision (\S+)/) {
			$curr_rev = $1;
		} else {
			warn("Expected \"revision: \", got \"$Line\".\"");
		}
		push(@Curr, $Line);
		$Line = <>;
		if ($Line =~ /^date: (\S+\s+\S+) .*/) {
			$curr_date = $1;
		} else {
			warn("Expected \"date: \", got \"$Line\".\"");
		}
		push(@Curr, $Line);
		# $Rev = unpack("H*", "$curr_date$curr_rev");
		$Rev = "$curr_date$curr_rev";
	} elsif ($Line =~ /^=============================================================================$/ && !$opt_a) {
		if (length($Rev) && scalar(@Curr)) {
			$Entry{$Rev} = join("", @Curr);
		}
		my @Arr = ();
		while (my ($l_name, $l_val) = each %Entry) {
			push(@Arr, $l_name);
		}
		for (sort @Arr) {
			print($Entry{$_});
		}
		($curr_date, $curr_rev, $Rev) = ("", "", "");
		%Entry = ();
		@Curr = ();
		print("$Line");
	} else {
		if (length($Rev)) {
			push(@Curr, $_);
		} else {
			print("$Line");
		}
	}
}

sub print_help {
	my $Retval = shift;
	print("No help yet.\n");
	exit($Retval);
}

=pod

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut

__END__
